package com.akeil.flash.action;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.akeil.flash.dataprocessing.IConstantProcessor;
import com.akeil.flash.dialog.BaseSettingDialog;
import com.akeil.flash.pojo.Other;
import com.akeil.flash.pojo.Parameter;
import com.akeil.flash.service.IOtherService;
import com.akeil.flash.service.IParameterService;
import com.akeil.flash.service.OtherServiceImpl;
import com.akeil.flash.service.ParameterServiceImpl;
import com.akeil.flash.view.ResultView;

import flash.Activator;
import flash.Application;

public class BaseSettingAction extends AbstractFlashAction {
	IParameterService parameterService = Application.applicationContext.getBean(ParameterServiceImpl.class);
	IPreferenceStore store = Activator.getDefault().getPreferenceStore();
	private DecimalFormat df2 = new DecimalFormat("0.00");
	List<Byte> cmds = new ArrayList<Byte>();
	List<Object> datas = new ArrayList<Object>();
	private Other theOther;
	
	public BaseSettingAction(){
		this.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/toolbar/settings_32px.png"));
		this.setText(resourceBundle.getString("toolbar.base.setting.name"));
		this.setToolTipText(resourceBundle.getString("toolbar.base.setting.tooltip"));
	}
	
	@Override
	public void run() {
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		BaseSettingDialog baseSettingDialog = new BaseSettingDialog(parentShell);
		if(IDialogConstants.OK_ID == baseSettingDialog.open()){
			List<Parameter> parameters = parameterService.selectAll();

			cmds.clear();
			datas.clear();
			cmds.add(IConstantProcessor.SettingTemp);
			datas.add(parameters.get(0).getPretemperature());
			
			cmds.add(IConstantProcessor.DelayTemp);
			datas.add(parameters.get(0).getNexttemperature());
			
			cmds.add(IConstantProcessor.TestStep);
			datas.add(parameters.get(0).getMethod().equals("A") ? new Float(0) : new Float(1));
			
			if(cmds != null && cmds.size() > 0){
				Application.processor.sendParameters(cmds,datas);
			}
			
			ResultView resultView = (ResultView)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ResultView.ID);
			if(resultView != null){
				resultView.setBaseSetting();
			}
		}
		super.run();
	}
	
	private Other findOtherByName(List<Other> others, String name){
		System.out.println("name : " + name);
		for (Other other : others) {
			if(name.equals(other.getName())){
				return other;
			}
		}
		return null;
	}


}
